package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.PanghuCourierStation;
import com.hualan.dao.StageDao1;
import com.hualan.dao.StageDaoImpl1;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/courierStation/querySearchCourierStationList")
public class QuerySearch extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String pageString = req.getParameter("page");
        int page = pageString == null ? 1 : Integer.parseInt(pageString);
        String sizeString = req.getParameter("size");
        int size = sizeString == null ? 5 : Integer.parseInt(sizeString);
        HashMap<String, Object> map = new HashMap<>();
        StageDao1 stageDao1 = new StageDaoImpl1();
        Long count = stageDao1.selectAllPanghuCourierStationCount();
        map.put("total", count);
        System.out.println("~~~~~~~~~~~~~~~~~~~");
        System.out.println(count);


        List<PanghuCourierStation> panghuCourierStationList = stageDao1.selectPanghuCourierStationWithPage(page, size);
        map.put("list", panghuCourierStationList);


        if (count > 0 && panghuCourierStationList != null){
            map.put("code", 200);
            map.put("message", "请求成功");
        }else {
            map.put("code", 300);
            map.put("message", "获取数据失败!");
        }

        String jsonString = JSON.toJSONString(map);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write(jsonString);
        System.out.println(jsonString);
    }

}
